package EjemplosEventosBasicos;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class EjemploWindowListener {
	
	private JDialog dialog;
	
	public void montarVentana (){
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.addWindowListener(new WindowAdapter(){
			public void windowIconified(WindowEvent e) {
				System.out.println("Iconificado");
			}
			
			public void windowDeiconified(WindowEvent e) {
				System.out.println("Deiconificado");
			}
		});
		
		JButton boton = new JButton("Crear ventana");
	
		dialog = new JDialog();
		crearVentana(dialog);
		
		boton.addActionListener(new ActionListener(){	
			public void actionPerformed(ActionEvent e) {
				dialog.setVisible(true);
			}
			
		});
		
		frame.add(boton);
		frame.setVisible(true);
		frame.pack();
	}
	
	public void crearVentana(JDialog dialog){
		dialog.addWindowListener(new WindowsListener(dialog));
		
		ImageIcon icon = new ImageIcon("java.jpg");
		JLabel label = new JLabel(icon);
		
		dialog.add(label);
		dialog.setVisible(false);
		dialog.pack();
	}
	
	public static void main (String[] args){
		EjemploWindowListener ewl = new EjemploWindowListener();
		ewl.montarVentana();
	}
}
